package com.xuecheng.manage_media.service;

import com.xuecheng.framework.domain.media.MediaFile;
import com.xuecheng.framework.domain.media.request.QueryMediaFileRequest;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.manage_media.dao.MediaFileRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MediaFileService {
    @Autowired
    MediaFileRepository mediaFileRepository;

    /**
     * @Description: 查询所有列表
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    public QueryResponseResult findList(int page, int size, QueryMediaFileRequest queryMediaFileRequest) {
        if (page <= 1) {
            page = 1;
        }
        if (size < 0) {
            size = 6;
        }
        //                分页查询
        page=page-1;
        Pageable pageable = new PageRequest(page, size);
        Page<MediaFile> all = null;
        if (queryMediaFileRequest != null) {
            String originalName = queryMediaFileRequest.getFileOriginalName();
            String processStatus = queryMediaFileRequest.getProcessStatus();
            String tag = queryMediaFileRequest.getTag();
//                定义查询条件
//                条件值对象
            MediaFile mediaFile = new MediaFile();

            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher(
                    "fileOriginalName",ExampleMatcher.GenericPropertyMatchers.contains()
            ).withMatcher(
                    "processStatus",ExampleMatcher.GenericPropertyMatchers.exact()
            );
            if (!StringUtils.isEmpty(originalName)) {
               mediaFile.setFileOriginalName(originalName);
            }
            if (!StringUtils.isEmpty(processStatus)) {
                mediaFile.setProcessStatus(processStatus);
            }
            if (!StringUtils.isEmpty(tag)) {
               mediaFile.setTag(tag);
            }

            Example<MediaFile> example = Example.of(mediaFile, matcher);

            all = mediaFileRepository.findAll(example, pageable);
        } else {
            all = mediaFileRepository.findAll(pageable);
        }
        QueryResult queryResult = new QueryResult();
        List<MediaFile> content = all.getContent();
        System.out.println(content);
        queryResult.setList(all.getContent());
        queryResult.setTotal(all.getTotalElements());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);

    }
}
